/**
  ******************************************************************************
  * @file    stm32f0xx_it.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    23-March-2012
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_it.h"
#include "main.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
//#define regulation


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
int dummy;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M0 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
}

/******************************************************************************/
/*                 STM32F0xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f0xx.s).                                               */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */

	void ADC1_COMP_IRQHandler(void)
{
	// Check if the interrupt is from ADC
	if (ADC_GetITStatus(ADC1, ADC_IT_EOC)==SET)
	{
	  STM_EVAL_LEDToggle(LED3);
		adc_reading = ADC_GetConversionValue(ADC1);	// get conversion value
		//DAC_SetChannel1Data(DAC_Align_12b_R, adc_reading*0.75);		// extra
		ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);	// clear pending bit
	}
	else
		// Interrupt is from the comparator. 
	{
		#ifndef regulation
			GPIOB->ODR = 0x00;
		#endif
		
		#ifdef regulation
			GPIOB->ODR= 0x03;
			dummy=0;
			dummy=1;
			dummy=2;
			dummy=3;
			dummy=4;
			dummy=5;
		#endif
		
//		GPIOB->ODR = Phase_state;
//		Phase_state ^= 0xFF;
		EXTI_ClearITPendingBit(EXTI_Line21);
		//EXTI->PR = EXTI_Line21;
	}
};



void TIM2_IRQHandler ()
{
	change_Frequency();
	STM_EVAL_LEDToggle(LED3);

	TIM_ClearITPendingBit(TIM2, TIM_IT_Update);	// clear pending bit
}


//void TIM3_IRQHandler ()
//{
//	ADC_StartOfConversion(ADC1);
//	TIM_ClearITPendingBit(TIM3, TIM_IT_Update);	// clear pending bit
//}

void TIM15_IRQHandler ()
{
	//GPIOB->ODR = 0x00;
#ifndef regulation

	Phase_index=(Phase_index+1)%4;
	GPIOB->ODR = Phase_state[Phase_index];
	for (x=0;x<5;x++)
	{
	}
	
	Phase_index++;
	GPIOB->ODR = Phase_state[Phase_index];

#endif

#ifdef regulation

	GPIOB->ODR = 0x00;
	GPIOB->ODR = 0x03;
	dummy=0;
	dummy=1;
	dummy=2;
	dummy=3;
//	dummy=4;
//	dummy=5;
//	dummy=4;
//	dummy=5;
	
//{
//	mov r2,#0x00;	// r2_v=r2_p
//	LDR r1,[__current_pc(),#28]; // r1_v=r0_p

//	LDR r5,[__current_pc(),#28]; // r5_v=r1_p
//	LDR r5,[r5,#0x00];
//	
//	STRH r2,[r1,#0x14];
//	STRH r5,[r1,#0x14];
//}
	//GPIOB->ODR= 0x00;
	GPIOB->ODR = Phase_state;

#endif
	
	//TIM_ClearITPendingBit(TIM15, TIM_IT_Update);
	TIM15->SR = (uint16_t)~TIM_IT_Update;	// clear pending bit	

}


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
